package com.ilop.siteraudio.view;

import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import java.util.List;

/**
 * @author skygge
 * @date 2021-01-31.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：
 */
public class MyPagerAdapter extends FragmentPagerAdapter {

    private List<Fragment> mFragmentList;
    private FragmentManager mManager;

    public MyPagerAdapter(@NonNull FragmentManager fm) {
        super(fm);
        this.mManager  = fm;
    }

    public MyPagerAdapter(@NonNull FragmentManager fm, List<Fragment> fragmentList) {
        super(fm);
        this.mManager  = fm;
        this.mFragmentList = fragmentList;
    }

    public void setFragment(List<Fragment> fragmentList) {
        this.mFragmentList = fragmentList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    @Override
    public int getItemPosition(@NonNull Object object) {
        return POSITION_NONE;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        Fragment fragment = (Fragment) super.instantiateItem(container, position);
        mManager.beginTransaction().show(fragment).commit();
        return fragment;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        Fragment fragment = mFragmentList.get(position);
        mManager.beginTransaction().hide(fragment).commit();
    }
}
